import CanvasAbstract from './canvasAbstract'
import config from '../config'
import model from '../model/tank'
import positions from '../service/position'

class Tank extends CanvasAbstract implements ICanvas {
  intervalId = 0
  num(): number {
    return config.tank.num
  }
  model(): ModelConstructor {
    return model
  }
  render(): void {
    this.createModels()
    super.renderModels()
    this.intervalId = setInterval(() => this.renderModels(), config.timeout)
  }
  // 批量产生坐标
  protected createModels() {
    for (let i = 0; i < this.num(); i++) {
      const pos = positions.position()
      const model = this.model()
      const instance = new model(pos.x, 0)
      this.models.push(instance)
    }
    // positions.positionCollection(this.num()).forEach((position) => {
    //   const model = this.model()
    //   const instance = new model(this.canvas, position.x, position.y)
    //   this.models.push(instance)
    // })
  }
  // 渲染模型到画布
  public override renderModels() {
    this.ctx.clearRect(0, 0, config.canvas.width, config.canvas.height)
    super.renderModels()
  }
  stop(): void {
    clearInterval(this.intervalId)
  }
}

export default new Tank('tank')
